package models;

import java.util.List;

import javax.persistence.*;
import play.db.ebean.*;
import play.db.ebean.Model.Finder;
import play.data.validation.Constraints.Required;

@Entity
public class Host extends Model{
	
	private static final long serialVersionUID = 2472394272148986245L;

	@Id
	public String name;
	public String description;
	public String owner;
	public String contact;
	@Required
	public String path;
	private String password;
	
	
	public static Finder<String, Host> find = new Finder<String, Host> (String.class, Host.class);
	
	
	public void setPassword(String password){
		this.password = password;
	}
	
	public static void create (Host host){
		host.save();
	}
		
	public static void remove (String name){
		find.byId(name).delete();
	}
	
	public static void remove (Host host){
		remove(host.name);
	}
	
	public static Host update(Host host, String oldPass){
		Host stored = find.byId(host.name);
		if (stored!=null){
			if (stored.password.equals(oldPass)){
				stored.name = host.name;
				stored.description = host.description;
				stored.owner = host.owner;
				stored.path = host.path;
				stored.password = host.password;
				stored.update();
				return stored;
			} else {
				return null;
			}
		} else {
			return null;
		}
	}
	
	public static List<Host> all(){
		return find.all();
	}

	public static Host find(String username) {
		return find.byId(username);
	}
	
}
